package foli

import (
	"fmt"
	"github.com/6tail/lunar-go/LunarUtil"
)

//(教日)每月不同
var MONTH_DIFF_DAYS = map[string][]string{
	"1-1":   {"天腊，玉帝校世人神气禄命"},
	"1-3":   {"万神都会"},
	"1-5":   {"五虚忌"},
	"1-6":   {"六耗忌"},
	"1-7":   {"上会日"},
	"1-8":   {"五殿阎罗天子诞"},
	"1-9":   {"玉皇上帝诞"},
	"1-13":  {"杨公忌"},
	"1-14":  {"三元降"},
	"1-15":  {"三元降", "上元神会"},
	"1-16":  {"三元降"},
	"1-19":  {"长春真人诞"},
	"1-23":  {"三尸神奏事"},
	"1-25":  {"天地仓开日"},
	"2-1":   {"一殿秦广王诞"},
	"2-2":   {"万神都会", "福德土地正神诞"},
	"2-3":   {"文昌帝君诞"},
	"2-6":   {"东华帝君诞"},
	"2-8":   {"释迦牟尼佛出家", "三殿宋帝王诞", "张大帝诞"},
	"2-11":  {"杨公忌"},
	"2-15":  {"释迦牟尼佛般涅槃", "太上老君诞"},
	"2-17":  {"东方杜将军诞"},
	"2-18":  {"四殿五官王诞", "至圣先师孔子讳辰"},
	"2-19":  {"观音大士诞"},
	"2-21":  {"普贤菩萨诞"},
	"3-1":   {"二殿楚江王诞"},
	"3-3":   {"玄天上帝诞"},
	"3-6":   {"六殿卞城王诞"},
	"3-9":   {"牛鬼神出", "杨公忌"},
	"3-12":  {"中央正道诞"},
	"3-15":  {"昊天上帝诞", "玄坛诞"},
	"3-16":  {"准提菩萨诞"},
	"3-19":  {"中岳大帝诞", "后土娘娘诞", "三茅降"},
	"3-20":  {"天地仓开日", "子孙娘娘诞"},
	"3-27":  {"七殿泰山王诞"},
	"3-28":  {"苍颉至圣先师诞"},
	"3-29":  {"东岳大帝诞"},
	"4-1":   {"八殿都市王诞"},
	"4-4":   {"万神善会", "文殊菩萨诞"},
	"4-7":   {"南斗、北斗、西斗同降", "杨公忌"},
	"4-8":   {"释迦牟尼佛诞", "万神善会", "善恶童子降", "九殿平等王诞"},
	"4-14":  {"纯阳祖师诞"},
	"4-15":  {"钟离祖师诞"},
	"4-16":  {"天地仓开日"},
	"4-17":  {"十殿转轮王诞"},
	"4-18":  {"天地仓开日", "紫徽大帝诞"},
	"4-20":  {"眼光圣母诞"},
	"5-1":   {"南极长生大帝诞"},
	"5-5":   {"地腊", "五帝校定生人官爵", "九毒日", "杨公忌"},
	"5-6":   {"九毒日"},
	"5-7":   {"九毒日"},
	"5-8":   {"南方五道诞"},
	"5-11":  {"天仓开日", "天下都城隍诞"},
	"5-12":  {"炳灵公诞"},
	"5-13":  {"关圣降"},
	"5-14":  {"夜子时为天地交泰"},
	"5-15":  {"九毒日"},
	"5-16":  {"九毒日", "天地元气造化万物之辰"},
	"5-17":  {"九毒日"},
	"5-18":  {"张天师诞"},
	"5-22":  {"孝娥神诞"},
	"5-25":  {"九毒日"},
	"5-26":  {"九毒日"},
	"5-27":  {"九毒日"},
	"6-1":   {"南极长生大帝诞"},
	"6-5":   {"地腊", "五帝校定生人官爵", "九毒日", "杨公忌"},
	"6-6":   {"九毒日"},
	"6-7":   {"九毒日"},
	"6-8":   {"南方五道诞"},
	"6-11":  {"天仓开日", "天下都城隍诞"},
	"6-12":  {"炳灵公诞"},
	"6-13":  {"关圣降"},
	"6-14":  {"夜子时为天地交泰"},
	"6-15":  {"九毒日"},
	"6-16":  {"九毒日", "天地元气造化万物之辰"},
	"6-17":  {"九毒日"},
	"6-18":  {"张天师诞"},
	"6-22":  {"孝娥神诞"},
	"6-25":  {"九毒日"},
	"6-26":  {"九毒日"},
	"6-27":  {"九毒日"},
	"7-1":   {" 杨公忌"},
	"7-5":   {"中会日"},
	"7-7":   {"道德腊", "五帝校生人善恶", "魁星诞"},
	"7-10":  {"阴毒日"},
	"7-12":  {"长真谭真人诞"},
	"7-13":  {"大势至菩萨诞"},
	"7-14":  {"三元降"},
	"7-15":  {"三元降", "地官校籍"},
	"7-16":  {"三元降"},
	"7-18":  {"西王母诞"},
	"7-19":  {"太岁诞"},
	"7-22":  {"增福财神诞"},
	"7-29":  {"杨公忌"},
	"7-30":  {"地藏菩萨诞"},
	"8-1":   {"许真君诞"},
	"8-3":   {"北斗诞", "司命灶君诞"},
	"8-5":   {"雷声大帝诞"},
	"8-10":  {"北斗大帝诞"},
	"8-12":  {"西方五道诞"},
	"8-15":  {"太明朝元"},
	"8-16":  {"天曹掠刷真君降"},
	"8-18":  {"天人兴福之辰"},
	"8-23":  {"汉恒候张显王诞"},
	"8-24":  {"灶君夫人诞"},
	"8-27":  {"至圣先师孔子诞", "杨公忌"},
	"8-29":  {"四天会事"},
	"8-30":  {"诸神考校"},
	"9-1":   {"南斗诞", "北斗九星降世"},
	"9-3":   {"五瘟神诞"},
	"9-9":   {"斗母诞", "酆都大帝诞", "玄天上帝飞升"},
	"9-10":  {"斗母降"},
	"9-11":  {"宜戒"},
	"9-13":  {"孟婆尊神诞"},
	"9-17":  {"金龙四大王诞"},
	"9-19":  {"日宫月宫会合", "观世音菩萨诞"},
	"9-25":  {"杨公忌"},
	"9-29":  {"药师琉璃光佛诞"},
	"10-1":  {"民岁腊", "四天王降"},
	"10-3":  {"三茅诞"},
	"10-5":  {"下会日", "达摩祖师诞"},
	"10-6":  {"天曹考察"},
	"10-8":  {"佛涅槃日"},
	"10-10": {"四天王降"},
	"10-11": {"宜戒"},
	"10-14": {"三元降"},
	"10-16": {"三元降"},
	"10-23": {"杨公忌"},
	"10-27": {"北极紫徽大帝降"},
	"11-4":  {"至圣先师孔子诞"},
	"11-6":  {"西岳大帝诞"},
	"11-11": {"天地仓开日", "太乙救苦天尊诞"},
	"11-17": {"阿弥陀佛诞"},
	"11-19": {"太阳日宫诞"},
	"11-21": {"杨公忌"},
	"11-23": {"张仙诞"},
	"11-25": {"掠刷大夫降"},
	"11-26": {"北方五道诞"},
	"12-6":  {"天地仓开日", "雷斋日"},
	"12-7":  {"掠刷大夫降"},
	"12-8":  {"王侯腊", "释迦如来成佛之辰", "王侯腊"},
	"12-12": {"太素三元君朝真"},
	"12-16": {"南岳大帝诞"},
	"12-19": {"杨公忌"},
	"12-20": {"天地交道"},
	"12-21": {"天猷上帝诞"},
	"12-23": {"五岳诞降"},
	"12-24": {"司今朝天奏人善恶"},
	"12-25": {"三清玉帝同降，考察善恶"},
	"12-29": {"华严菩萨诞 "},
	"12-30": {"诸神下降，察访善恶"},
}

//(教日)每月同
var MONTH_SAME_DAYS = map[int][]string{
	1:  {"月朔"},
	3:  {"斗降"},
	6:  {"雷斋日"},
	8:  {"四天王巡行"},
	14: {"四天王巡行"},
	15: {"月朔", "四天王巡行"},
	23: {"月晦", "四天王巡行"},
	27: {"斗降"},
	28: {"人神在阴"},
	29: {"月晦", "四天王巡行"},
	30: {"司命奏事", "四天王巡行"},
}

//观音斋
var FAST_GUAN_YIN = []string{
	"1-8",
	"2-7", "2-9", "2-19",
	"3-3", "3-6", "3-13",
	"4-12",
	"5-3", "5-17",
	"6-16", "6-18", "6-19", "6-23",
	"7-13",
	"8-16",
	"9-13", "9-23",
	"10-2",
	"11-19", "11-24",
	"12-25",
}

type Foli struct {
}

/**
 * @Description: 佛历年月日
 * @receiver fo
 * @param year 年(公历)
 * @return bool true:是 false:否
 */
func (fo *Foli) GetFoliYear(year int) int {
	return year + 544
}

/**
 * @Description: 是否是观音斋
 * @receiver fo
 * @param month 月(农历)
 * @param day 日(农历)
 * @return bool true:是 false:否
 */
func (fo *Foli) IsFastGuanYin(month int, day int) bool {
	res := false
	mdfKey := fmt.Sprintf("%d-%d", month, day)
	for _, value := range FAST_GUAN_YIN {
		if mdfKey == value {
			res = true
			break
		}
	}

	return res
}

/**
 * @Description: 是否是十日斋(又名地藏斋)
 * @receiver fo （每月）初一日、初八日、十四日、十五日、十八日、廿三日、廿四日、廿八日、廿九日、三十日（月小从廿七日起）
 * @param year 年(农历)
 * @param month 月(农历)
 * @param day 日(农历)
 * @return bool true:是 false:否
 */
func (fo *Foli) IsFastTenDay(year int, month int, day int) bool {
	res := false
	//小月从27日起
	if count := LunarUtil.GetDaysOfMonth(year, month); count == 29 && day == 27 {
		res = true
	}

	//大月
	if day == 1 || day == 8 || day == 14 || day == 15 || day == 18 || day == 23 || day == 24 || day == 28 || day == 29 || day == 30 {
		res = true
	}

	return res
}

/**
 * @Description: 是否是六斋日
 * @receiver fo （每月）初八日、十四日、十五日、廿三日、廿九日、三十日（月小从廿八日起）。
 * @param year 年(农历)
 * @param month 月(农历)
 * @param day 日(农历)
 * @return bool true:是 false:否
 */
func (fo *Foli) IsFastSixDay(year int, month int, day int) bool {
	res := false
	//小月从28日起
	if count := LunarUtil.GetDaysOfMonth(year, month); count == 29 && day == 28 {
		res = true
	}

	//大月
	if day == 8 || day == 14 || day == 15 || day == 23 || day == 29 || day == 30 {
		res = true
	}

	return res
}

/**
 * @Description: 是否是斋月
 * @receiver fo （每年）正月、五月、九月。
 * @param month 月(农历)
 * @return bool true:是 false:否
 */
func (fo *Foli) IsFastMonth(month int) bool {
	return month == 1 || month == 5 || month == 9
}

/**
 * @Description: 是否朔望斋
 * @receiver fo （每月）初一日、十五日。
 * @param day 日(农历)
 * @return bool true:是 false:否
 */
func (fo *Foli) IsFastSyzygy(day int) bool {
	return day == 8 || day == 14
}

/**
 * @Description: 获取日期的斋日
 * @receiver fo
 * @param year 年(农历)
 * @param month 月(农历)
 * @param day 日(农历)
 * @return []string 斋日名称数组
 * @return bool 是否是斋日
 */
func (fo *Foli) GetFast(year int, month int, day int) ([]string, bool) {
	res := []string{}
	//是否是观音斋
	if ok := fo.IsFastGuanYin(month, day); ok {
		res = append(res, "观音斋")
	}
	//是否是十日斋(地藏斋)
	if ok := fo.IsFastTenDay(year, month, day); ok {
		res = append(res, "十日斋")
	}
	//是否是六日斋
	if ok := fo.IsFastSixDay(year, month, day); ok {
		res = append(res, "六日斋")
	}
	//是否是朔望斋
	if ok := fo.IsFastSyzygy(day); ok {
		res = append(res, "朔望斋")
	}
	//是否是斋月
	if ok := fo.IsFastMonth(month); ok {
		res = append(res, "月斋")
	}
	return res, len(res) > 0
}

/**
 * @Description: 获取佛教日
 * @receiver fo
 * @param year 年(农历)
 * @param month 月(农历)
 * @param day 日(农历)
 * @return []string 佛教日名称数组
 * @return bool 是否是佛教日
 */
func (fo *Foli) GetReligionDays(year int, month int, day int) ([]string, bool) {
	var res []string
	//每月同
	if val, ok := MONTH_SAME_DAYS[day]; ok {
		res = append(res, val...)
	}

	//每月不同
	mdfKey := fmt.Sprintf("%d-%d", month, day)
	if val, ok := MONTH_DIFF_DAYS[mdfKey]; ok {
		res = append(res, val...)
	}

	return res, len(res) > 0
}

/**
 * @Description: 获取佛历
 * @receiver fo
 * @param year 年(农历)
 * @param month 月(农历)
 * @param day 日(农历)
 * @return []string 佛历名称数组
 */
func (fo *Foli) GetFoliDays(year int, month int, day int) ([]string, bool) {
	var res []string
	//每月同
	if val, ok := fo.GetReligionDays(year, month, day); ok {
		res = append(res, val...)
	}

	if val, ok := fo.GetFast(year, month, day); ok {
		res = append(res, val...)
	}

	return res, len(res) > 0
}
